﻿using System;
using System.Text.RegularExpressions;
using KKCai.TV;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

namespace KKCai.Job
{
    internal class MyJob : Quartz.IJob
    {

        #region IJob 成员
        //private const string url = "http://www.hnflcp.com/moreKjinfo2.asp?pageno=1&cptype=5";
        //public void Execute(Quartz.JobExecutionContext context)
        //{
        //    string resp = new WebUtils().DoGet(url);

        //    int startIndex = resp.IndexOf("开奖号码");
        //    int endIndex = resp.IndexOf("上一页");

        //    resp = resp.Substring(startIndex, endIndex - startIndex);

        //    Regex reg_issue = new Regex(@">(\d{7})<");
        //    Regex reg_item = new Regex(@">(\d{2})<");

        //    MatchCollection mc_issue = reg_issue.Matches(resp);
        //    MatchCollection mc_item = reg_item.Matches(resp);

        //    string issue = String.Format("2013{0}", mc_issue[0].Groups[1].Value);

        //    string items = String.Empty;

        //    for (int i = 0; i < 8; i++)
        //    {
        //        items += String.Format("{0} ", mc_item[i].Groups[1].Value);
        //    }
        //    items = items.Trim();

        //    new Manager().Save(issue, items);            
        //}

        #endregion

        #region IJob 成员
        private const string url = "http://222.240.177.52/wincode/wincode_play_gd.jsp";
        public void Execute(Quartz.JobExecutionContext context)
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();

            dict.Add("get_wincode_submit_ex_ex", "1");
            dict.Add("row", "1");

            string phase, result;
            try
            {
                string resp = new WebUtils().DoPost(url, dict).Trim();

                JObject o = JObject.Parse(resp);
                string code = (string)o.SelectToken("sp_priceinfor[0].Win_base_code"); ;

                result = Regex.Replace(code, "\\d{2}", "$0 ").TrimEnd();
                phase = "2013" + (string)o.SelectToken("sp_priceinfor[0].term_code");
            }
            catch { return; }

            new Manager().Save(phase, result); 
        }

        #endregion
    }
}
